<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>检查文档是否存在 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/doc-exists.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/doc-exists.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/doc-exists.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/doc-exists.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="getting-started.html">基础入门</a></span>
»
<span class="breadcrumb-link"><a href="data-in-data-out.html">数据输入和输出</a></span>
»
<span class="breadcrumb-node">检查文档是否存在</span>
</div>
<div class="navheader">
<span class="prev">
<a href="get-doc.html">« 取回一个文档</a>
</span>
<span class="next">
<a href="update-doc.html">更新整个文档 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="doc-exists"></a>检查文档是否存在<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/030_Data/20_Exists.asciidoc">edit</a>
</h2>
</div></div></div>
<p>如果只想检查一个文档是否存在--根本不想关心内容—​那么用 <code class="literal">HEAD</code> 方法来代替 <code class="literal">GET</code> 方法。 <code class="literal">HEAD</code> 请求没有返回体，只返回一个 HTTP 请求报头：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">curl -i -XHEAD http://localhost:9200/website/blog/123</pre>
</div>
<p>如果文档存在， Elasticsearch 将返回一个 <code class="literal">200 ok</code> 的状态码：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">HTTP/1.1 200 OK
Content-Type: text/plain; charset=UTF-8
Content-Length: 0</pre>
</div>
<p>若文档不存在， Elasticsearch 将返回一个 <code class="literal">404 Not Found</code> 的状态码：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">curl -i -XHEAD http://localhost:9200/website/blog/124</pre>
</div>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">HTTP/1.1 404 Not Found
Content-Type: text/plain; charset=UTF-8
Content-Length: 0</pre>
</div>
<p>当然，一个文档仅仅是在检查的时候不存在，并不意味着一毫秒之后它也不存在：也许同时正好另一个进程就创建了该文档。</p>
</div>
<div class="navfooter">
<span class="prev">
<a href="get-doc.html">« 取回一个文档</a>
</span>
<span class="next">
<a href="update-doc.html">更新整个文档 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>